﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GTauto.Site.FrontEnd.Controllers
{
    public class ImgController : Controller
    {
        public ActionResult GetInSize7534(string id)
        {
            Graphics graphicsObj;
            Image newImage1 = Image.FromFile(System.Web.HttpContext.Current.Request.MapPath(string.Format("\\Uploads\\BrendLogos\\{0}", id)));
            Graphics g = Graphics.FromImage(newImage1);
            
            Size size = new Size();
            size.Width = 75;
            size.Height = 34;
            newImage1 = ResizeImage(newImage1, size);
            var temp = ImageToByteArray(newImage1);

            return File(temp, "image/jpeg", "fileName.jpg");
        }

        public ActionResult GetInSize9971(string id)
        {
            Graphics graphicsObj;
            Image newImage1 = Image.FromFile(System.Web.HttpContext.Current.Request.MapPath(string.Format("\\Uploads\\BrendLogos\\{0}", id)));
            Graphics g = Graphics.FromImage(newImage1);
            Size size = new Size();
            size.Width = 99;
            size.Height = 71;
            newImage1 = ResizeImage(newImage1, size);
            var temp = ImageToByteArray(newImage1);

            return File(temp, "image/jpeg", "fileName.jpg");
        }


        public ActionResult GetInSize5930(string id)
        {
            Graphics graphicsObj;
            Image newImage1 = Image.FromFile(System.Web.HttpContext.Current.Request.MapPath(string.Format("\\Uploads\\BrendLogos\\{0}", id)));
            Graphics g = Graphics.FromImage(newImage1);
            Size size = new Size();
            size.Width = 59;
            size.Height = 30;
            newImage1 = ResizeImage(newImage1, size);
            var temp = ImageToByteArray(newImage1);

            return File(temp, "image/jpeg", "fileName.jpg");
        }

        public ActionResult GetInSize181127(string id)
        {
            Graphics graphicsObj;
            Image newImage1 = Image.FromFile(System.Web.HttpContext.Current.Request.MapPath(string.Format("\\Uploads\\BrendLogos\\{0}", id)));
            Graphics g = Graphics.FromImage(newImage1);
            Size size = new Size();
            size.Width = 181;
            size.Height = 127;
            newImage1 = ResizeImage(newImage1, size);
            var temp = ImageToByteArray(newImage1);

            return File(temp, "image/jpeg", "fileName.jpg");
        }


        public ActionResult GetInSize105122(string id)
        {
            Graphics graphicsObj;
            Image newImage1 = Image.FromFile(System.Web.HttpContext.Current.Request.MapPath(string.Format("\\Uploads\\BrendLogos\\{0}", id)));
            Graphics g = Graphics.FromImage(newImage1);
            Size size = new Size();
            size.Width = 105;
            size.Height = 122;
            newImage1 = ResizeImage(newImage1, size);
            var temp = ImageToByteArray(newImage1);

            return File(temp, "image/jpeg", "fileName.jpg");
        }


        public ActionResult GetInSize195(string id)
        {
            Graphics graphicsObj;
            Image newImage1 = Image.FromFile(System.Web.HttpContext.Current.Request.MapPath(string.Format("\\Uploads\\BrendLogos\\{0}", id)));
            Graphics g = Graphics.FromImage(newImage1);
            Size size = new Size();
            size.Width = 195;
            size.Height = 195;
            newImage1 = ResizeImage(newImage1, size);
            var temp = ImageToByteArray(newImage1);

            return File(temp, "image/jpeg", "fileName.jpg");
        }



        public static byte[] ImageToByteArray(System.Drawing.Image imageIn)
        {
            var ms = new MemoryStream();
            imageIn.Save(ms, ImageFormat.Png);
            return ms.ToArray();
        }

        protected static Image ResizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.High;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }
        

    }
}
